﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExpressionLanguage;

namespace Example4
{
    class Program
    {
        static void Main(string[] args)
        {
            // Right now, you may think it is just an fancier calculator. What if I want to control a specific part of the code 
            // that can be executed when a certain flag is on. Suppose that you ship the product in release version, develop version.
            // For develop version you have some extra data to be loaded. (Apache Server configuration is a example).
            // We need branch indicator, and accidentally it provides.

            // 1. Initialize Everything
            ExpressionInterpreter.ExecutionStack stack = new ExpressionInterpreter.ExecutionStack();
            ExpressionInterpreter.GlobalFunction func = new ExpressionInterpreter.GlobalFunction();
            CoreLibrary.LoadLibrary(func, stack);
            MathLibrary.LoadLibrary(func, stack);
            StringLibrary.LoadLibrary(func, stack);

            // 2. Write script

            string script = "DEBUG=2;" +
                "if(DEBUG==1){" +
                "AA=1;BB=2;}elif(DEBUG==2){ AA=4;BB=4; } else{AA=2;BB=1;}";

            // 3. Execute
            ErrorCollector error = ExpressionInterpreter.Execute(script, func, stack);

            double AA, BB;

            if ( !stack.LookUpAsNumber("AA",out AA) || !stack.LookUpAsNumber("BB",out BB))
                return; // Kidding me!!

            // 4. See what's happening
            Console.Write("AA:{0}\nBB:{1}", AA, BB);

            return;
        }
    }
}
